import json

from PyQt6.QtCore import QThreadPool

from midjourney_config import *

from scripts import db_utils
from scripts.WorkSingle import Worker
from scripts.gpts.Gpt_Utils import reasoning_prompts


def reasoning(index, itemholders, callback):
    project_config = db_utils.load_project_config()
    type = project_config['reasoning_platform']
    userid = db_utils.load_project_config()['userid']
    text = ""
    if index < 0:
        for index, holder in enumerate(itemholders):
            text = text + f'{index + 1}.“'
            for srt in holder.bean.srtbean:
                text = text + srt['text'] + '，'
            if index < len(itemholders) - 1:
                text = text + '。” \n '
            else:
                text = text + '。”'

    else:
        text = f'{index + 1}.“'
        for srt in itemholders[index].bean.srtbean:
            text = text + srt['text'] + '”'

    worker = Worker(inner=lambda :reasoning_prompts(type, userid, userid, text), callback=callback)
    thread = QThreadPool.globalInstance()
    thread.start(worker)
